################################################
## CONJOINT ANALYSIS - UNITED KINGDOM        ###
################################################


rm(list=ls(all=TRUE))
detach(UK.conj)

### Load libraries
library(arm)
library(stargazer)
library(sjPlot)
library(ggplot2)
library(ggthemes)
library(foreign)
library(mediation)
library(cregg)
library(FindIt)


### Read and attach data

#Find your path

UK.conj <- read.csv("C:/Users/gmagni/OneDrive/SideProjects/3-AndyNEW/00-Outlin&Paper-Appendix/00-Latest/00-JOP/R&R/FinalSubmission/Data/UK-LGBT-Conj.csv")


attach(UK.conj)
n = nrow(UK.conj)

###################




### Code variables


## Conjoint variables: candidate choice

table(CJ.1.Rat.1_1)
rating.B.uk <- as.numeric(as.character(CJ.1.Rat.1_1))
table(rating.B.uk)


table(CJ.1.ChoiceDummy)
dummy.choice.B.uk <- as.numeric(as.character(CJ.1.ChoiceDummy))
table(dummy.choice.B.uk)

table(CJ.1.REC_1)
race.B.uk = rep(NA,n)
race.B.uk[CJ.1.REC_1=="Asian"]="Asian"
race.B.uk[CJ.1.REC_1=="Black"]="Black"
race.B.uk[CJ.1.REC_1=="White"]="White"
table(race.B.uk)

race.B.uk.factor <- factor(race.B.uk, levels = c("White", "Asian", "Black"))
table(race.B.uk.factor)


table(CJ.1.REC_2)
sexorient.B.uk = rep(NA,n)
sexorient.B.uk[CJ.1.REC_2=="Straight"]="Straight"
sexorient.B.uk[CJ.1.REC_2=="Gay"]="Gay"
table(sexorient.B.uk)

sexorient.B.uk.factor <- factor(sexorient.B.uk, levels = c("Straight", "Gay"))
table(sexorient.B.uk.factor)


table(CJ.1.REC_3)
religion.B.uk = rep(NA,n)
religion.B.uk[CJ.1.REC_3=="Not religious"]="Not religious"
religion.B.uk[CJ.1.REC_3=="Christian"]="Christian"
religion.B.uk[CJ.1.REC_3=="Muslim"]="Muslim"
table(religion.B.uk)

religion.B.uk.factor <- factor(religion.B.uk, levels = c("Not religious", "Christian", 
                                                         "Muslim"))
table(religion.B.uk.factor)


table(CJ.1.REC_4)
age.B.uk = rep(NA,n)
age.B.uk[CJ.1.REC_4=="35"]="35"
age.B.uk[CJ.1.REC_4=="44"]="44"
age.B.uk[CJ.1.REC_4=="56"]="56"
age.B.uk[CJ.1.REC_4=="71"]="71"
table(age.B.uk)

age.B.uk.factor <- factor(age.B.uk, levels = c("35", "44", "56", "71"))
table(age.B.uk.factor)


table(CJ.1.REC_5)
health.B.uk = rep(NA,n)
health.B.uk[CJ.1.REC_5=="In a wheelchair since birth"]="Weelchair since birth"
health.B.uk[CJ.1.REC_5=="Healthy"]="Healthy"
health.B.uk[CJ.1.REC_5=="Overweight, has diabetes"]="Overweight, diabetes"
health.B.uk[CJ.1.REC_5=="HIV positive"]="HIV+"
health.B.uk[CJ.1.REC_5=="HIV positive since birth"]="HIV+ since birth"
table(health.B.uk)

health.B.uk.factor <- factor(health.B.uk, levels = c("Healthy", "HIV+", "Weelchair since birth", 
                                                     "Overweight, diabetes",
                                                     "HIV+ since birth"))
table(health.B.uk.factor)


table(CJ.1.REC_6)
polexp.B.uk = rep(NA,n)
polexp.B.uk[CJ.1.REC_6=="No previous experience"]="No experience"
polexp.B.uk[CJ.1.REC_6=="Town council member"]="Town council member"
polexp.B.uk[CJ.1.REC_6=="Member of the House of Commons"]="House of Commons"
table(polexp.B.uk)

polexp.B.uk.factor <- factor(polexp.B.uk, levels = c("No experience", 
                                                     "Town council member", 
                                                     "House of Commons"))
table(polexp.B.uk.factor)


table(CJ.1.REC_7)
educ.B.uk = rep(NA,n)
educ.B.uk[CJ.1.REC_7=="Less than high school"]="No high school"
educ.B.uk[CJ.1.REC_7=="High school degree"]="High school"
educ.B.uk[CJ.1.REC_7=="Bachelor's degree"]="Bachelor"
educ.B.uk[CJ.1.REC_7=="Master degree"]="Master"
table(educ.B.uk)

educ.B.uk.factor <- factor(educ.B.uk, levels = c("No high school", "High school", 
                                                     "Bachelor", "Master"))
table(educ.B.uk.factor)


table(CJ.1.REC_8)
gender.B.uk = rep(NA,n)
gender.B.uk[CJ.1.REC_8=="Man"]="Man"
gender.B.uk[CJ.1.REC_8=="Woman"]="Woman"
gender.B.uk[CJ.1.REC_8=="Transgender"]="Transgender"
table(gender.B.uk)

gender.B.uk.factor <- factor(gender.B.uk, levels = c("Man", "Transgender", "Woman"))
table(gender.B.uk.factor)


## Causal mechanisms 

table(CJ.1.CM_LeftDummy) #left-leaning
cm.left.leaning = rep(NA,n)
cm.left.leaning[CJ.1.CM_LeftDummy=="0"]=0
cm.left.leaning[CJ.1.CM_LeftDummy=="1"]=1
table(cm.left.leaning)

table(CJ.1.CM_NeighborDummy) #prefer as neighbor
cm.pref.neighb = rep(NA,n)
cm.pref.neighb[CJ.1.CM_NeighborDummy=="0"]=0
cm.pref.neighb[CJ.1.CM_NeighborDummy=="1"]=1
table(cm.pref.neighb)

table(CJ.1.CM_WinDummy) #better chances to win election
cm.electorab = rep(NA,n)
cm.electorab[CJ.1.CM_WinDummy=="0"]=0
cm.electorab[CJ.1.CM_WinDummy=="1"]=1
table(cm.electorab)



## Controls


table(IncomeH)
income.h.uk = rep(NA,n)
income.h.uk[IncomeH=="No income"]=0
income.h.uk[IncomeH=="Less than  £5.000"]=1
income.h.uk[IncomeH=="£5.000- £10.000"]=2
income.h.uk[IncomeH=="£10.000- £15.000"]=3
income.h.uk[IncomeH=="£15.000- £20.000"]=4
income.h.uk[IncomeH=="£20.000- £25.000"]=5
income.h.uk[IncomeH=="£25.000- £30.000"]=6
income.h.uk[IncomeH=="£30.000- £40.000"]=7
income.h.uk[IncomeH=="£40.000- £50.000"]=8
income.h.uk[IncomeH=="£50.000- £60.000"]=9
income.h.uk[IncomeH=="£60.000- £70.000"]=10
income.h.uk[IncomeH=="£70.000- £85.000"]=11
income.h.uk[IncomeH=="£85.000- £100.000"]=12
income.h.uk[IncomeH=="£100.000- £150.000"]=13
income.h.uk[IncomeH=="£150.000- £200.000"]=14
income.h.uk[IncomeH=="£200.000- £250.000"]=15
income.h.uk[IncomeH=="More than  £250.000"]=16
table(income.h.uk)

table(Educ)
educ.uk = rep(NA,n)
educ.uk[Educ=="No high school"]=0
educ.uk[Educ=="Some high school but no diploma"]=1
educ.uk[Educ=="High school graduate"]=2
educ.uk[Educ=="Some university"]=3
educ.uk[Educ=="Bachelor's degree"]=4
educ.uk[Educ=="Postgraduate degree (for example Master, PhD, ...)"]=5
table(educ.uk)

table(GenderID)
female.uk = rep(NA,n)
female.uk[GenderID=="Woman"]=1
female.uk[GenderID=="Man"]=0
table(female.uk)
prop.table(table(female.uk))

table(YOB)
age.uk<-2018-YOB
table(age.uk)
mean(age.uk, na.rm=TRUE)
median(age.uk, na.rm=TRUE)

table(PolIdeol.Eco_1)
eco.right.uk <- as.numeric(as.character(PolIdeol.Eco_1))
table(eco.right.uk)
prop.table(table(eco.right.uk))

table(PolIdeol.Soc_1)
soc.conserv.uk <- as.numeric(as.character(PolIdeol.Soc_1))
table(soc.conserv.uk)
prop.table(table(soc.conserv.uk))

table(VoteChoice)
party.choice.uk = rep(NA,n)
party.choice.uk[VoteChoice=="British National Party (BNP)"]="BNP"
party.choice.uk[VoteChoice=="Conservative Party"]="Conserv"
party.choice.uk[VoteChoice=="Green Party"]="Green"
party.choice.uk[VoteChoice=="Labour Party"]="Labour"
party.choice.uk[VoteChoice=="Liberal Democrats"]="LibDem"
party.choice.uk[VoteChoice=="Plaid Cymru"]="PC"
party.choice.uk[VoteChoice=="Scottish National Party (SNP)"]="SNP"
party.choice.uk[VoteChoice=="United Kingdom Independence Party (UKIP)"]="UKIP"
party.choice.uk[VoteChoice=="Other"]="Other"
#party.choice.uk[Vote.Y.N=="No, I didn't vote"]="NoVote"
table(party.choice.uk)
prop.table(table(party.choice.uk))

voted.ukip <- as.numeric(VoteChoice=="United Kingdom Independence Party (UKIP)") #based on all voters
table(voted.ukip)

table(KnowLGBT)
know.lgbt = rep(NA,n)
know.lgbt[KnowLGBT=="Yes"]=1
know.lgbt[KnowLGBT=="No"]=0
table(know.lgbt)

table(SexOrient)
lgbt.self = rep(NA,n)
lgbt.self[SexOrient=="Gay" | SexOrient=="Lesbian" | SexOrient=="Bisexual" | SexOrient=="Other"]=1
lgbt.self[SexOrient=="Straight"]=0
table(lgbt.self)

table(ReligY.N)
religious.yes = rep(NA,n)
religious.yes[ReligY.N=="Yes"]=1
religious.yes[ReligY.N=="No"]=0
table(religious.yes)

table(ReligID) #okay

table(Religiosity)
religiosity = rep(NA,n)
religiosity[Religiosity=="Never"]=0
religiosity[Religiosity=="Only on special holidays"]=1
religiosity[Religiosity=="At least once a month"]=2
religiosity[Religiosity=="Once a week"]=3
religiosity[Religiosity=="Every day"]=4
table(religiosity)



table(Race)
prop.table(table(Race))
race = rep(NA,n)
race[Race=="White"]="white"
race[Race=="Black/African/Caribbean/Black British"]="black"
race[Race=="Black/African/Caribbean/Black British,Other"]="black"
race[Race=="Black/African/Caribbean/Black British,White"]="black"
race[Race=="Asian/Asian British: Indian"]="asian"
race[Race=="Asian/Asian British: Pakistani"]="asian"
race[Race=="Asian/Asian British: Pakistani,White"]="asian"
table(race)
prop.table(table(race))

race.factor <- factor(race, levels = c("white", "black", "asian"))
table(race.factor)



# Attach newly created variables to dataset


UK.conj$eco.right.uk <- eco.right.uk
UK.conj$soc.conserv.uk <- soc.conserv.uk
UK.conj$job.insec.uk <- job.insec.uk
UK.conj$income.h.uk <- income.h.uk
UK.conj$educ.uk <- educ.uk
UK.conj$female.uk <- female.uk
UK.conj$age.uk <- age.uk
UK.conj$party.choice.uk <- party.choice.uk
UK.conj$att.check.passed <- att.check.passed

UK.conj$dummy.choice.B.uk <- dummy.choice.B.uk
UK.conj$age.B.uk <- age.B.uk
UK.conj$age.B.uk.factor <- age.B.uk.factor
UK.conj$religion.B.uk <- religion.B.uk
UK.conj$religion.B.uk.factor <- religion.B.uk.factor
UK.conj$polexp.B.uk <- polexp.B.uk
UK.conj$polexp.B.uk.factor <- polexp.B.uk.factor
UK.conj$health.B.uk <- health.B.uk
UK.conj$health.B.uk.factor <- health.B.uk.factor
UK.conj$sexorient.B.uk <- sexorient.B.uk
UK.conj$sexorient.B.uk.factor <- sexorient.B.uk.factor
UK.conj$educ.B.uk <- educ.B.uk
UK.conj$educ.B.uk.factor <- educ.B.uk.factor
UK.conj$race.B.uk <- race.B.uk
UK.conj$race.B.uk.factor <- race.B.uk.factor
UK.conj$gender.B.uk <- gender.B.uk
UK.conj$gender.B.uk.factor <- gender.B.uk.factor
UK.conj$know.lgbt <- know.lgbt
UK.conj$lgbt.self <- lgbt.self
UK.conj$religious.yes <- religious.yes
UK.conj$ReligID <- ReligID
UK.conj$religiosity <- religiosity

UK.conj$cm.left.leaning <- cm.left.leaning
UK.conj$cm.pref.neighb <- cm.pref.neighb
UK.conj$cm.electorab <- cm.electorab

UK.conj$state.uk <- state.uk


### Analysis

### Using [cregg] ###

#Rename conjoint attribute variables to have clearer plots and attach them to dataset 

Sexual_Orientation <- sexorient.B.uk.factor
Gender_Identity <- gender.B.uk.factor
Education <- educ.B.uk.factor
Race <- race.B.uk.factor
Political_Experience <- polexp.B.uk.factor
Religion <- religion.B.uk.factor
Age <- age.B.uk.factor
Health <- health.B.uk.factor

UK.conj$Sexual_Orientation <- Sexual_Orientation
UK.conj$Gender_Identity <- Gender_Identity
UK.conj$Education <- Education
UK.conj$Race <- Race
UK.conj$Political_Experience <- Political_Experience
UK.conj$Religion <- Religion
UK.conj$Age <- Age
UK.conj$Health <- Health

Choice <- dummy.choice.B.uk
UK.conj$Choice <- Choice


#Create dummy variables for subgroup analysis and attach them to dataset

Labour = rep(NA,n) #vs. Conserv
Labour[party.choice.uk=="Labour"]=1
Labour[party.choice.uk=="Conserv"]=0 
table(Labour)
UK.conj$Labour <- Labour

Progressive = rep(NA,n)
Progressive[soc.conserv.uk==0 | soc.conserv.uk==1 | soc.conserv.uk==2]=1
Progressive[soc.conserv.uk==8 | soc.conserv.uk==9 | soc.conserv.uk==10]=0
table(Progressive)
UK.conj$Progressive <- Progressive

LGBT.friends <-know.lgbt
table(LGBT.friends)
UK.conj$LGBT.friends <- LGBT.friends

Women<-female.uk
table(Women)
UK.conj$Women <- Women

Young = rep(NA,n)
Young[age.uk<="35"]=1
Young[age.uk>="60"]=0
table(Young)
UK.conj$Young <- Young

Non.religious = rep(NA,n)
Non.religious[religiosity==0]=1
Non.religious[religiosity==3 | religiosity==4]=0
table(Non.religious)
UK.conj$Non.religious <- Non.religious


## Subset of dataset 

UK.conj.labour <- subset(UK.conj, Labour == 1)
UK.conj.conserv <- subset(UK.conj, party.choice.uk=="Conserv")

UK.conj.lgbtvoters <- subset(UK.conj, lgbt.self == 1)
UK.conj.straightvoters <- subset(UK.conj, lgbt.self == 0)


### ACME ###

#ACME - General
amce(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                 Health + Age + Religion + Political_Experience + 
                 Race + Education, id = ~ ResponseId)

amce.1 <- amce(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                 Health + Age + Religion + Political_Experience + 
                 Race + Education, id = ~ ResponseId)


plot(amce.1, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank())


### MM ###

mm.1 <- mm(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
             Health + Age + Religion + Political_Experience + 
             Race + Education, id = ~ ResponseId, h0 = 0.5)

plot(mm.1, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank()) + geom_vline(xintercept = 0.50, colour="gray33")




## SUBGROUP ANALYSIS


# AMCE by groups (just change dataset in command)

#LGBT vs. straight voters
amce.lgbtvoters <- amce(UK.conj.lgbtvoters, Choice ~ Sexual_Orientation + Gender_Identity +  
                 Health + Age + Religion + Political_Experience + 
                 Race + Education, id = ~ ResponseId)

amce.straightvoters <- amce(UK.conj.straightvoters, Choice ~ Sexual_Orientation + Gender_Identity +  
                 Health + Age + Religion + Political_Experience + 
                 Race + Education, id = ~ ResponseId)


# MMs split by groups

#MM by gender
stacked.gender <- cj(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                       Health + Age + Religion + Political_Experience + 
                       Race + Education, id = ~ ResponseId,
                     estimate = "mm", by = ~ Women)

#MM by party
stacked.party <- cj(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                       Health + Age + Religion + Political_Experience + 
                       Race + Education, id = ~ ResponseId,
                     estimate = "mm", by = ~ Labour)

#MM by political ideology
stacked.polideology <- cj(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                       Health + Age + Religion + Political_Experience + 
                       Race + Education, id = ~ ResponseId,
                     estimate = "mm", by = ~ Progressive)

#MM by having LGBT friends
stacked.lgbtfriends <- cj(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                       Health + Age + Religion + Political_Experience + 
                       Race + Education, id = ~ ResponseId,
                     estimate = "mm", by = ~ LGBT.friends)

#MM by age
stacked.age <- cj(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                       Health + Age + Religion + Political_Experience + 
                       Race + Education, id = ~ ResponseId,
                     estimate = "mm", by = ~ Young)

#MM by religiosity
stacked.religiosity <- cj(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                       Health + Age + Religion + Political_Experience + 
                       Race + Education, id = ~ ResponseId,
                     estimate = "mm", by = ~ Non.religious)

##MM by sexual orientation
stacked.sexor <- cj(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                            Health + Age + Religion + Political_Experience + 
                            Race + Education, id = ~ ResponseId,
                          estimate = "mm", by = ~ lgbt.self)


### SUBGROUP DIFFERENCES ###

### ACME differences
acme.diff.1 <- amce_diffs(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                            Health + Age + Religion + Political_Experience + 
                            Race + Education, ~Women , id = ~ ResponseId)

plot(acme.diff.1, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank())


### MM differences

#mm differences by gender
mm.diff.gender <- mm_diffs(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                             Health + Age + Religion + Political_Experience + 
                             Race + Education, 
                           ~ Women, id = ~ ResponseId)
plot(mm.diff.gender, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank())

#mm differences by party
mm.diff.party <- mm_diffs(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                            Health + Age + Religion + Political_Experience + 
                            Race + Education, 
                          ~ Labour, id = ~ ResponseId)
plot(mm.diff.party, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank())

#mm differences by political ideology
mm.diff.polideol <- mm_diffs(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                               Health + Age + Religion + Political_Experience + 
                               Race + Education, 
                             ~ Progressive, id = ~ ResponseId)
plot(mm.diff.polideol, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank())

#mm differences by having LGBT friends
mm.diff.lgbtfriends <- mm_diffs(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                                  Health + Age + Religion + Political_Experience + 
                                  Race + Education, 
                                ~ LGBT.friends, id = ~ ResponseId)
plot(mm.diff.lgbtfriends, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank())

#mm differences by age
mm.diff.age <- mm_diffs(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                          Health + Age + Religion + Political_Experience + 
                          Race + Education, 
                        ~ Young, id = ~ ResponseId)
plot(mm.diff.age, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank())

#mm differences by religiosity
mm.diff.religiosity <- mm_diffs(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                                  Health + Age + Religion + Political_Experience + 
                                  Race + Education, 
                                ~ Non.religious, id = ~ ResponseId)
plot(mm.diff.religiosity, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank())

#mm differences by sexual orientation
mm.diff.sexor <- mm_diffs(UK.conj, Choice ~ Sexual_Orientation + Gender_Identity +  
                                  Health + Age + Religion + Political_Experience + 
                                  Race + Education, 
                                ~ lgbt.self, id = ~ ResponseId)




## Analysis for causal mechanism


#Causal mediation

library(mediation)

med.fit <- lm(cm.pref.neighb ~ gender.B.uk.factor + sexorient.B.uk.factor + 
                 health.B.uk.factor + age.B.uk.factor + religion.B.uk.factor + polexp.B.uk.factor + 
                 race.B.uk.factor + educ.B.uk.factor +
                 age.uk + educ.uk + female.uk + know.lgbt + party.choice.uk +
                 race + religiosity + income.h.uk, data = UK.conj)
out.fit <- lm(dummy.choice.B.uk ~ cm.pref.neighb + gender.B.uk.factor + sexorient.B.uk.factor + 
                health.B.uk.factor + age.B.uk.factor + religion.B.uk.factor + polexp.B.uk.factor + 
                race.B.uk.factor + educ.B.uk.factor +
                age.uk + educ.uk + female.uk + know.lgbt + party.choice.uk +
                race + religiosity + income.h.uk, data = UK.conj) 

med.out <- mediate(med.fit, out.fit, treat = "sexorient.B.uk.factor", 
                   mediator = "cm.pref.neighb", 
                   sims = 1000,
                   dropobs = TRUE,
                   robustSE = TRUE)
summary(med.out)
plot(med.out)


#Sensitivity analysis
sens.out <- medsens(med.out, rho.by = 0.1, effect.type = "indirect", sims = 100)
summary(sens.out)



### Causal mechanisms with mechanisms as outcome variables

#ACME electability
amce.cm.1 <- amce(UK.conj, cm.electorab ~ Sexual_Orientation + Gender_Identity +  
                 Health + Age + Religion + Political_Experience + 
                 Race + Education, id = ~ ResponseId)

plot(amce.cm.1, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank()) + 
  xlim(-0.13, 0.10)


#ACME prejudice
amce.cm.2 <- amce(UK.conj, cm.pref.neighb ~ Sexual_Orientation + Gender_Identity +  
                    Health + Age + Religion + Political_Experience + 
                    Race + Education, id = ~ ResponseId)

plot(amce.cm.2, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank()) +
  xlim(-0.13, 0.10)


#ACME identity left
amce.cm.3 <- amce(UK.conj, cm.left.leaning ~ Sexual_Orientation + Gender_Identity +  
                    Health + Age + Religion + Political_Experience + 
                    Race + Education, id = ~ ResponseId)

plot(amce.cm.3, feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, ncol = 1L,
                      scales = "free_y", strip.position = "right") + 
  theme(legend.title = element_blank()) +
  theme(strip.text = element_blank()) +
  xlim(-0.13, 0.10)



# Electability with prejudice
conjoint.cm.elector.prej <- lm(cm.electorab ~ gender.B.uk.factor + sexorient.B.uk.factor + 
                                 health.B.uk.factor +
                                 age.B.uk.factor + religion.B.uk.factor + polexp.B.uk.factor + 
                                 race.B.uk.factor +
                                 educ.B.uk.factor + cm.pref.neighb)
summary(conjoint.cm.elector.prej)



## INTERACTION EFFECTS: FINDIT PACKAGE

#Subset so you don't have missing data
myvars2 <- c("dummy.choice.B.uk", "religion.B.uk.factor", "gender.B.uk.factor", "sexorient.B.uk.factor",
             "health.B.uk.factor", "educ.B.uk.factor", "age.B.uk.factor", "religion.B.uk.factor",
             "polexp.B.uk.factor", "race.B.uk.factor", 
             "ResponseId")
UK.conj.LGBT.inter <- UK.conj[myvars2]
UK.conj.LGBT.inter <- na.omit(UK.conj.LGBT.inter)

## Specify the order of each factor
UK.conj.LGBT.inter$gender.B.uk.factor<- factor(UK.conj.LGBT.inter$gender.B.uk.factor,ordered=TRUE,
                                               levels=c("Woman", "Transgender", "Man"))
UK.conj.LGBT.inter$sexorient.B.uk.factor<- factor(UK.conj.LGBT.inter$sexorient.B.uk.factor,ordered=TRUE,
                                                  levels=c("Straight", "Gay"))
UK.conj.LGBT.inter$race.B.uk.factor<- factor(UK.conj.LGBT.inter$race.B.uk.factor,ordered=TRUE,
                                             levels=c("Black", "Asian", "White"))

#Calculate AMIE 

#Gender and sexual orientation 
fit.1 <- CausalANOVA(formula=dummy.choice.B.uk ~ gender.B.uk.factor + sexorient.B.uk.factor + 
                       health.B.uk.factor +
                       age.B.uk.factor + religion.B.uk.factor + polexp.B.uk.factor + 
                       race.B.uk.factor +
                       educ.B.uk.factor,
                     int2.formula = ~ gender.B.uk.factor:sexorient.B.uk.factor,
                     data=UK.conj.LGBT.inter, 
                     cluster=UK.conj.LGBT.inter$ResponseId, nway=2)
summary(fit.1)

#Sexual orientation and race
fit.2 <- CausalANOVA(formula=dummy.choice.B.uk ~ gender.B.uk.factor + sexorient.B.uk.factor + 
                       health.B.uk.factor +
                       age.B.uk.factor + religion.B.uk.factor + polexp.B.uk.factor + 
                       race.B.uk.factor +
                       educ.B.uk.factor,
                     int2.formula = ~ sexorient.B.uk.factor:race.B.uk.factor,
                     data=UK.conj.LGBT.inter, 
                     cluster=UK.conj.LGBT.inter$ResponseId, nway=2)
summary(fit.2)


#Gender and race
UK.conj.LGBT.inter$gender.B.uk.factor<- factor(UK.conj.LGBT.inter$gender.B.uk.factor,ordered=TRUE,
                                               levels=c("Woman", "Man", "Transgender"))
fit.3 <- CausalANOVA(formula=dummy.choice.B.uk ~ gender.B.uk.factor + sexorient.B.uk.factor + 
                       health.B.uk.factor +
                       age.B.uk.factor + religion.B.uk.factor + polexp.B.uk.factor + 
                       race.B.uk.factor +
                       educ.B.uk.factor,
                     int2.formula = ~ gender.B.uk.factor:race.B.uk.factor,
                     data=UK.conj.LGBT.inter, 
                     cluster=UK.conj.LGBT.inter$ResponseId, nway=2)
summary(fit.3)


#Health and race
UK.conj.LGBT.inter$health.B.uk.factor<- factor(UK.conj.LGBT.inter$health.B.uk.factor,ordered=TRUE,
                                               levels=c("HIV+ since birth", "Weelchair since birth", 
                                                        "Healthy", "Overweight, diabetes", "HIV+"))
fit.4 <- CausalANOVA(formula=dummy.choice.B.uk ~ gender.B.uk.factor + sexorient.B.uk.factor + 
                       health.B.uk.factor +
                       age.B.uk.factor + religion.B.uk.factor + polexp.B.uk.factor + 
                       race.B.uk.factor +
                       educ.B.uk.factor,
                     int2.formula = ~ health.B.uk.factor:race.B.uk.factor,
                     data=UK.conj.LGBT.inter, 
                     cluster=UK.conj.LGBT.inter$ResponseId, nway=2)
summary(fit.4)
